#pragma once 
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;

//日志等级分类
enum LogLevel
{
    INFO = 0,
    WARNING,
    ERROR,
    FATAL,
    DEBUG 
};

//时间戳的计算
class TimeStamp 
{
    public:
        static void GetTimeStamp(string* timestamp)
        {
            time_t t;
            time(&t);//t获得现在到1990年的总秒数
            struct tm* tmp = localtime(&t);//返回的tm结构体包含年月日时分秒
            char buf[30] = {'\0'};
            //yyyy-mm-dd HH:MM:SS
            snprintf(buf,sizeof(buf)-1,"%04d-%02d-%02d %02d:%02d:%2d",tmp->tm_year+1900,tmp->tm_mon+1,tmp->tm_yday,tmp->tm_hour,tmp->tm_min,tmp->tm_sec);    
            timestamp->assign(buf,strlen(buf));
        }
};

const char* Loglevel[] ={
    "INFO",
    "WARNING",
    "ERROR",
    "FATAL",
    "DEBUG" 
};
//[时间戳 日志等级 文件名:行号 ] 信息  
ostream& LOG(LogLevel loglevel,const char* file,int line,string msg)
{
    string timestamp;
    TimeStamp::GetTimeStamp(&timestamp);
    cout << '[' << timestamp << ' ' << Loglevel[loglevel] << ' ' << file << ':' << line << ' ' << ']' << ' ' << msg; 
    return cout;
}
#define LOG(loglevel,msg) LOG(loglevel,__FILE__,__LINE__,msg)
